﻿using Ldt.DataEngine.DataFlowParse.Model;
using LdtJudge.DataEngine.Common;
using LdtJudge.DataEngine.Common.XmlUtility;
using LdtJudge.DataEngine.DataBaseStores;
using LdtJudge.DataEngine.Model.DataModel.SysCommon;
using LdtJudge.DataEngine.Model.XMLConfigModel.ConfigModel;
using LdtJudge.DataEngine.Model.XMLConfigModel.DataStores;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LdtDataEngin.ModuleDetail.CommonModule.CourtCountGenerate
{
    /// <summary>
    /// 庭次补全处理
    /// </summary>
    public class CourtCountGenerateDetail
    {


        private DataContext dataContext { get; }

        private string modleNmae { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dataContextIn">上下文</param>
        /// <param name="moudleName">模块名称</param>
        public CourtCountGenerateDetail(DataContext dataContextIn, string moudleName)
        {
            modleNmae = moudleName;
            dataContext = dataContextIn;
        }
        /// <summary>
        /// 庭次补充
        /// </summary>
        public void Complement()
        {

            ConfigModel moudelConfig = dataContext.DataLayerSys.GetConfigModel(modleNmae);
            ConfigModel dataConfig = moudelConfig.ConfigDetail?.FirstOrDefault(x => x.Code.ToLower() == "numberconfig");
            if (dataConfig!=null)
            {
                NumberConfigModel numberConfigModel = XmlUtility.DeserializeToObject<NumberConfigModel>(dataConfig.ConfigContent);
                if (numberConfigModel != null)
                {
                    CaseTypeConfig caseTypeConfig = numberConfigModel.caseTypeConfigList.FirstOrDefault(a => a.value == dataContext.DataLayerAns.CaseType && a.booktype.ToLower() == dataContext.DataLayerAns.SourceCode.ToLower());
                    if (caseTypeConfig.typedReferenceList != null && caseTypeConfig.typedReferenceList.Count > 0)
                    {
                        foreach (TtargetDataConfig typedReference in caseTypeConfig.typedReferenceList)
                        {
                            #region 庭次推导
                            dataContext.InteractiveShow.LogShow("------------------进入庭次推导模块---------------------");
                            // 获取当前的庭次与开庭时间

                            CourtNoDeduce courtNoDeduce = new CourtNoDeduce(dataContext);
                            string courtNO = courtNoDeduce.DeduceCourtNo(dataContext.DataLayerAns.AnalysisResultsDataModel.dataModelList, dataContext.DataCommon.CaseId, typedReference);
                            switch (courtNO.Split(':')[0])
                            {
                                case "N"://推导出的庭次
                                    foreach (ResultDataConfig resultData in typedReference.resultDataConfigList)
                                    {
                                        string path = resultData.Xmlpath;
                                        List<DataModel> dataModelList = new List<DataModel>();
                                        if (dataContext.DataLayerAns.AnalysisResultsDataModel.dataModelList.FirstOrDefault(a => a.XmlPath == path) != null)
                                        {
                                            dataModelList = dataContext.DataLayerAns.AnalysisResultsDataModel.dataModelList.Where(a => a.XmlPath == path).ToList();
                                            for (int i = 0; i < dataModelList.Count; i++)
                                            {
                                                DataModel dataModel = dataModelList[i];
                                                if (dataModel == null && path.IndexOf("法庭使用记录") > -1)
                                                {
                                                    XmlUtilCommon xmlUtilCommon = new XmlUtilCommon() { dataContext = this.dataContext };
                                                    string id = xmlUtilCommon.CaseNodeUpdate(dataContext.DataLayerAns.AnalysisResultsDataModel.dataModelList, path);
                                                    dataModel = dataContext.DataLayerAns.AnalysisResultsDataModel.dataModelList.FirstOrDefault(a => a.ID == id);
                                                }
                                                if (dataModel != null && dataModel.AttributesList.FirstOrDefault(a => a.Name == resultData.Number) == null)
                                                {
                                                    AttributeInfo attribute = new AttributeInfo();
                                                    attribute.Name = resultData.Number;
                                                    attribute.sourceList = new List<Source>();
                                                    Source source = new Source();
                                                    source.SaveValue = courtNO.Split(':')[1];
                                                    source.IsCheck = "1";
                                                    source.AnalysisStatus = "1";
                                                    source.SourceCode = dataContext.DataLayerAns.SourceCode;
                                                    attribute.sourceList.Add(source);
                                                    dataModel.AttributesList.Add(attribute);
                                                }
                                                else if (dataModel != null && dataModel.AttributesList.FirstOrDefault(a => a.Name == resultData.Number).sourceList.FirstOrDefault(a => a.SourceCode.ToLower() == dataContext.DataLayerAns.SourceCode.ToLower()).SaveValue == "")
                                                {
                                                    dataModel.AttributesList.FirstOrDefault(a => a.Name == resultData.Number).sourceList.FirstOrDefault(a => a.SourceCode.ToLower() == dataContext.DataLayerAns.SourceCode.ToLower()).SaveValue = courtNO.Split(':')[1];
                                                }
                                            }
                                        }

                                    }
                                    dataContext.InteractiveShow.LogShow("推导出庭次为:" + courtNO.Split(':')[1]);

                                    break;
                                case "U"://无开庭日期无开庭次数废弃掉
                                    dataContext.InteractiveShow.LogShow("#######未提出庭次也没有提出开庭时间，无效文本#######");
                                    break;
                                case "E"://已经提取出了庭次
                                    dataContext.InteractiveShow.LogShow("#######提出庭次不在进行庭次推导#######");
                                    break;
                            }
                            dataContext.InteractiveShow.LogShow("------------------庭次推导模块处理完成-------------------");
                            #endregion
                        }

                    }
                }
            }
          

        }
    }
}
